﻿Imports Tajan.Monitoring.Repository
Imports Tajan.Monitoring.Entities

Public Class ConnectionService
    Inherits BaseService

    Private connectionRepository As ConnectionRepository
    Private domainRepository As DomainRepository
    Private pageViewRepository As PageViewRepository

    Public Sub New(reporsitoryFactory As RepositoryFactory)
        MyBase.New(reporsitoryFactory)

        Me.connectionRepository = Me.ReporsitoryFactory.GetRepositiry(Of ConnectionRepository)()
        Me.domainRepository = Me.ReporsitoryFactory.GetRepositiry(Of DomainRepository)()
        Me.pageViewRepository = Me.ReporsitoryFactory.GetRepositiry(Of PageViewRepository)()

    End Sub

    Public Sub CreateNewConnection(connection As Connection)
        Me.connectionRepository.UpdateExpiredConnections()
        Me.connectionRepository.Add(connection)
        Me.ReporsitoryFactory.Commit()
    End Sub

    Public Function NewPageView(pageViewInfo As Client.PageViewInfo, serverUsername As String) As PageView

        Dim connection = Me.connectionRepository.GetById(pageViewInfo.ConnectionId)

        connection.EndDate = Now
        connection.IsAvtive = True

        'check if connection has screen data or not
        If connection.Screen Is Nothing Then
            connection.Screen = New Screen
            Me.FillScreen(connection.Screen, pageViewInfo, connection.ConnectionId)
        End If

        'check if user connection exists
        Dim userConnection As UserConnection = (From p In connection.UserConnections Where p.ClientUsername = pageViewInfo.ClientUsername AndAlso p.ServerUsername = serverUsername).SingleOrDefault

        If userConnection Is Nothing Then
            userConnection = New UserConnection
            Me.FillUserConnection(userConnection, pageViewInfo, serverUsername)
            connection.UserConnections.Add(userConnection)
        Else
            userConnection.EndDate = Now
        End If


        Dim url As New Uri(pageViewInfo.Url)
        Dim domain As Domain = Me.GetDomainByName(url.DnsSafeHost)
        Dim pageView As New PageView

        Me.FillPageView(pageView, pageViewInfo, domain, userConnection, url)

        pageViewRepository.Add(pageView)

        Me.ReporsitoryFactory.Commit()

        pageViewInfo.PageViewId = pageView.PageViewId

        Return pageView

    End Function

    Private Sub FillScreen(screen As Screen, pageViewInfo As Client.PageViewInfo, connectionId As Long)

        With screen
            .ConnectionId = connectionId
            .AvailHeight = pageViewInfo.Screen.AvailHeight
            .AvailLeft = pageViewInfo.Screen.AvailLeft
            .AvailTop = pageViewInfo.Screen.AvailTop
            .AvailWidth = pageViewInfo.Screen.AvailWidth
            .ColorDepth = pageViewInfo.Screen.ColorDepth
            .Height = pageViewInfo.Screen.Height
            .PixelDepth = pageViewInfo.Screen.PixelDepth
            .Width = pageViewInfo.Screen.Width
        End With

    End Sub

    Private Sub FillPageView(pageView As PageView, pageViewInfo As Client.PageViewInfo, domain As Domain, userConnection As UserConnection, url As Uri)

        Dim path As String = url.AbsolutePath
        Dim page As String = url.Segments(url.Segments.Count - 1)
        Dim port As Integer = url.Port
        Dim scheme As String = url.Scheme

        With pageView
            .ClientLoadDuaration = pageViewInfo.ClientLoadDuration
            .Domain = domain
            .EndDate = Now
            .Path = path
            .Page = page
            .Port = port
            .Scheme = scheme
            .QueryString = url.Query
            .RawUrl = pageViewInfo.Url
            .ServerLoadDuration = pageViewInfo.ServerLoadDuration
            .UserConnection = userConnection
            .StartDate = Now
            .ClientUsername = pageViewInfo.ClientUsername
            .ServerUsername = userConnection.ServerUsername
        End With

    End Sub

    Private Sub FillUserConnection(userConnection As UserConnection, pageViewInfo As Client.PageViewInfo, serverUsername As String)

        With userConnection
            .ConnectionId = pageViewInfo.ConnectionId
            .ServerUsername = serverUsername
            .ClientUsername = pageViewInfo.ClientUsername
            .StartDate = Now
            .EndDate = Now
        End With

    End Sub

    Private Function GetDomainByName(domainName As String) As Domain
        'check if domain name exists
        Dim domain As Domain = Me.domainRepository.GetByName(domainName)
        If domain Is Nothing Then
            domain = New Domain
            domain.Name = domainName
            Me.DomainRepository.Add(domain)
        End If
        Return domain
    End Function

    Public Function GetConnectionById(connectionId As Long) As Connection
        Return Me.connectionRepository.GetById(connectionId)
    End Function

    Public Function GetLiveConnections() As List(Of Connection)
        Return Me.connectionRepository.GetLiveConnections()
    End Function

    Public Sub Disconnected(connectionId As Long)
        Me.connectionRepository.Disconnected(connectionId)
    End Sub

End Class
